package com.shixu.framework.plugins.weixin.service;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.shixu.framework.plugins.weixin.util.HttpKit;


/**
 * 高级群发接口
 * @author luy
 *
 */
public class BulkMsgService {
	
	/** 文本消息 */
	public static final String TYPE_TEXT = "text";
	/** 图文消息 */
	public static final String TYPE_MPNEWS = "mpnews";
	
	/** 根据分组进行群发连接 */
	public final String SEND_ALL_URI = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=";
	public final String SEND_ONE_URI = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=";
	
	/**
	 * 根据分组进行群发文本消息
	 * @param access_token 微信凭证
	 * @param group_id 分组id
	 * @param content 消息内容
	 * @return
	 * @throws IOException
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public JSONObject sendAllText(String access_token,String group_id,String content) throws IOException, ExecutionException, InterruptedException{
		String result = null;		  
		result = HttpKit.post(SEND_ALL_URI + access_token, getDataForAllOfText(group_id, content));
		if(StringUtils.isNotEmpty(result)){
    		return JSONObject.fromObject(result);
    	}
        return null;  
	}
	
	/**
	 * 根据分组进行群发文本消息
	 * @param access_token 微信凭证
	 * @param group_id 分组id
	 * @param content 消息内容
	 * @return
	 * @throws IOException
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public JSONObject sendAllMpnews(String access_token,String group_id,String media_id) throws IOException, ExecutionException, InterruptedException{
		String result = null;		  
		result = HttpKit.post(SEND_ALL_URI + access_token, getDataForAllOfMpnews(group_id, media_id));
		if(StringUtils.isNotEmpty(result)){
    		return JSONObject.fromObject(result);
    	}
        return null;  
	}
	
	/**
	 * 根据OpenID列表群发文本消息
	 * @param access_token 微信凭证
	 * @param group_id 分组id
	 * @param content 消息内容
	 * @return
	 * @throws IOException
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public JSONObject sendOneText(String access_token,String[] openids,String content) throws IOException, ExecutionException, InterruptedException{
		String result = null;		  
		result = HttpKit.post(SEND_ONE_URI + access_token, getDataForOneOfText(openids, content));
		if(StringUtils.isNotEmpty(result)){
    		return JSONObject.fromObject(result);
    	}
        return null; 
	}
	
	/**
	 * 根据OpenID列表群发文本消息
	 * @param access_token 微信凭证
	 * @param group_id 分组id
	 * @param content 消息内容
	 * @return
	 * @throws IOException
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public JSONObject sendOneMpnews(String access_token,String[] openids,String media_id) throws IOException, ExecutionException, InterruptedException{
		String result = null;		  
		result = HttpKit.post(SEND_ONE_URI + access_token, getDataForOneOfMpnews(openids, media_id));
		if(StringUtils.isNotEmpty(result)){
    		return JSONObject.fromObject(result);
    	}
        return null;  
	}
	
	
	/**
	 * 构造文本消息，根据分组群发
	 * @param group_id
	 * @param content
	 * @return
	 */
	private String getDataForAllOfText(String group_id,String content) {
		/*
		 {
		   "filter":{
		      "is_to_all":false
		      "group_id":"2"
		   },
		   "text":{
		      "content":"CONTENT"
		   },
		    "msgtype":"text"
		}
		 */
		JSONObject filterJO = new JSONObject();
		filterJO.put("is_to_all", false);
		filterJO.put("group_id", group_id);
		
		JSONObject textJO = new JSONObject();
		textJO.put("content", content);
		
		JSONObject message = new JSONObject();
		message.put("filter", filterJO);
		message.put("text", textJO);
		message.put("msgtype", TYPE_TEXT);
		
		return message.toString();
	}
	
	/**
	 * 构造图文消息，根据分组群发
	 * @param group_id
	 * @param media_id
	 * @return
	 */
	private String getDataForAllOfMpnews(String group_id,String media_id) {
		/*
		 {
		   "filter":{
		      "is_to_all":false
		      "group_id":"2"
		   },
		   "mpnews":{
		      "media_id":"123dsdajkasd231jhksad"
		   },
		    "msgtype":"mpnews"
		}
		 */
		JSONObject filterJO = new JSONObject();
		filterJO.put("is_to_all", false);
		filterJO.put("group_id", group_id);
		
		JSONObject mpnewsJO = new JSONObject();
		mpnewsJO.put("media_id", media_id);
		
		JSONObject message = new JSONObject();
		message.put("filter", filterJO);
		message.put("mpnews", mpnewsJO);
		message.put("msgtype", TYPE_MPNEWS);
		
		System.out.println(message.toString());
		
		return message.toString();
	}
	
	/**
	 * 构造文本消息，根据OpenID列表群发
	 * @param openids
	 * @param content
	 * @return
	 */
	private String getDataForOneOfText(String[] openids,String content) {
		/*
		 {
		   "touser":[
		    "OPENID1",
		    "OPENID2"
		   ],
		    "msgtype": "text",
		    "text": { "content": "hello from boxer."}
		}
		 */
		JSONObject textJO = new JSONObject();
		textJO.put("content", content);
		
		JSONObject message = new JSONObject();
		message.put("touser", openids);
		message.put("msgtype", TYPE_TEXT);
		message.put("text", textJO);
		
		return message.toString();
	}
	
	/**
	 * 构造图文消息，根据OpenID列表群发
	 * @param openids
	 * @param media_id
	 * @return
	 */
	private String getDataForOneOfMpnews(String[] openids,String media_id) {
		/*
		 {
		   "touser":[
		    "OPENID1",
		    "OPENID2"
		   ],
		   "mpnews":{
		      "media_id":"123dsdajkasd231jhksad"
		   },
		    "msgtype":"mpnews"
		}
		 */
		JSONObject mpnewsJO = new JSONObject();
		mpnewsJO.put("media_id", media_id);
		
		JSONObject message = new JSONObject();
		message.put("touser", openids);
		message.put("mpnews", mpnewsJO);
		message.put("msgtype", TYPE_MPNEWS);
		
		return message.toString();
	}

	
	  

	/**
	 * 测试
	 * @param args
	 * @throws IOException
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */
	public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
		//实例化操作类
		BulkMsgService news = new BulkMsgService();
		
		//定义返回值
		String result = null;
		
		String access_token = "fWbIL3Gy1pPkF9aNIXWzV9CreOH9wxV8GELTchOBvDvFv2jFYB9RyKmMr9phXYSYYctufMK6n3IahwRKVC7h0FjZqlKxH0A_nEo8HI-6nSU";
		String media_id = "f95HfRvxwAwj2wyxiKEph_CsxZ4fXHLvXnvoTdMTImAZFHZSvz6Y1T4X62lxVNoz";
		
		/* 根据分组群发文本消息 */
		result = news.sendAllText(access_token, "0", "测试根据分组群发文本消息 ").toString();
	    System.out.println("测试根据分组群发文本消息 "+result); 
	    
	    /* 根据分组群发图文消息 */
		result = news.sendAllMpnews(access_token, "0", media_id).toString();
	    System.out.println("测试根据分组群发图文消息 "+result); 
	    
	    String[] openids = {"o9QgNuOSzalLmFlNonrP6QciGEk4","o9QgNuDF7-3lY4nH1yuLRF-jRhvg"};
	    
	    /* 根据根据OpenID列表群发文本消息 */
		result = news.sendOneText(access_token, openids, "测试根据根据OpenID列表群发文本消息").toString();
	    System.out.println("测试根据根据OpenID列表群发文本消息"+result); 
	    
	    /* 根据根据OpenID列表群发图文消息 */
		result = news.sendOneMpnews(access_token, openids, media_id).toString();
	    System.out.println("测试根据根据OpenID列表群发图文消息"+result); 
	    
	    
	  
	}  

}
